package gang.org.springframework.framework.bean;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author gang.chen
 * @since  2021/6/12 20:29
 */
public abstract class GangBeanUtils {

    /**
     * 业务意义
     * Class对象实例化
     * */
    public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws Exception {
        try {
            Class<?> [] parameterTypes = ctor.getParameterTypes();
            /**
             * 1，从构造器，获取构造方法Class实例
             * 2，将实例保存到数组
             * */
            Object[] argsWithDefaultValues = new Object[parameterTypes.length];
            for (int i = 0; i < args.length; i++) {
                argsWithDefaultValues[i] = args[i];
            }
            return ctor.newInstance(argsWithDefaultValues);
        }
        catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            System.out.println(e.getMessage());
            throw new Exception("实例化对象异常...");
        }
    }

    public static <T> T instantiateClass(Class<T> clazz){
        try {
            return instantiateClass(clazz.getDeclaredConstructor());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz){

        return null;
    }
}
